package com.internship.javawebinternship.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.internship.javawebinternship.entity.*;
import com.internship.javawebinternship.myUtils.UnicodeUtil;
import com.internship.javawebinternship.repository.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/notification")
public class NotificationController {
    @Autowired
    private NotificationRepository notificationRepository;
    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private StudentInfoRepository studentInfoRepository;
    @Autowired
    private TutorInfoRepository tutorInfoRepository;
    @Autowired
    private OtherEmployeeInfoRepository otherEmployeeInfoRepository;

    @PostMapping("showNotReadNum")//显示未读通知的数量
    public Object showNotReadNum(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        deleteDeprecateNotificationByNo(no);
        try {
            int res = notificationRepository.countByStuempnoAndReadedEquals(no, 0);//获取某个用户的未读通知的数量
            result.put("result", "1");
            result.put("num", String.valueOf(res));
        } catch (Exception e) {
            result.put("result", "0");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("showList")//显示每条通知的详情
    public Object showList(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> result2 = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Object res = notificationRepository.findByStuempno(no);//查找是否有通知
        Object[] obj = new Object[((ArrayList<?>) res).size()];
        if (!((ArrayList<?>) res).isEmpty()) {//有通知
            try {
                //循环，获取通知内容，放到集合result2中
                for (int i = 0; i < ((ArrayList<?>) res).size(); i++) {

                    Notification notification = notificationRepository.findByStuempnoOrderByReadedDescDateDescImportantDesc(no).get(i);
                    Account account = accountRepository.findByStuempno(notification.getFromstuempno()).get(0);
                    int notifyID = notification.getNotifyid();
                    String notifyHeader = notification.getNotifyheader();
                    String notifyContent = notification.getNotifycontent();
                    String date = notification.getDate();
                    int read = notification.getReaded();
                    String from = notification.getFromstuempno();
                    from = accountRepository.findByStuempno(from).get(0).getName();
                    int important = notification.getImportant();

                    result.clear();
                    result.put("notifyID", String.valueOf(notifyID));
                    result.put("notifyHeader", notifyHeader);
                    result.put("fromName", account.getName());
                    result.put("notifyContent", notifyContent);
                    result.put("date", date);
                    result.put("from", from);
                    result.put("important", String.valueOf(important));
                    result.put("read", String.valueOf(read));

                    result2.put("result", "1");
                    obj[i] = JSON.toJSON(result);
                }
            } catch (IndexOutOfBoundsException e) {
                //没有更多通知了，将result的结果位置0
                result.clear();
                result.put("result", "0");

                result2.put("data", obj);//将获取到的数据result，转换为JSON格式，存到result2中
                JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
                return object;//返回json数据给前端
            }
            //用户未收到任何通知
        } else {
            result2.put("result", "1");
            result2.put("msg", "你暂时还没有收到通知呢");
        }

//        if (!((ArrayList<?>) res).isEmpty()) {//有通知
//            try {
//                //获取通知内容，放到集合result2中
//                List<Notification> notification;
//
//                notification = notificationRepository.findByStuempnoOrderByReadedDescDateDescImportantDesc(no);
//
//                result2.put("result", "1");
//                result2.put("data", notification);//将获取到的数据result，转换为JSON格式，存到result2中
//
//            } catch (IndexOutOfBoundsException e) {
//                //没有更多通知了，直接返回数据给前端
//                JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
//                return object;//返回json数据给前端
//            }
//            //用户未收到任何通知
//        } else {
//            result2.put("result", "1");
//            result2.put("msg", "你暂时还没有收到通知呢");
//        }
        result2.put("data", obj);
        JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("readOne")//将通知标记为已读
    public Object readContent(@RequestBody String frontEnd,
                              HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }
        HttpSession session = request.getSession(false);

        try {
            session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String notifyID = receiveJSON.getString("notifyid");

        try {
            Notification notification = notificationRepository.findById(notifyID).get();
            notification.setReaded(1);
            notificationRepository.save(notification);
            result.put("notifyContent", notification.getNotifycontent());
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        result.put("result", "1");
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("readAll")//将全部通知设为已读
    public Object readAll(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            notificationRepository.findByStuempnoSetAllReaded1(no);
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        result.put("result", "1");
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("deleteOne")//将某条通知删除
    public Object deleteOne(@RequestBody String frontEnd,
                            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        HttpSession session = request.getSession(false);

        try {
            session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String notifyID = receiveJSON.getString("notifyid");

        try {
            Notification notification = notificationRepository.findById(notifyID).get();
            notificationRepository.deleteById(String.valueOf(notification.getNotifyid()));
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        result.put("result", "1");
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("deleteAll")//将全部通知删除
    public Object deleteAll(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        HttpSession session = request.getSession(false);

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            notificationRepository.deleteByStuempno(no);
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        result.put("result", "1");
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("showSendByMeList")
    public Object showSendByMeList(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Map<String, Object> result2 = new HashMap<>();
        HttpSession session = request.getSession(false);

        String fromNo;
        try {
            fromNo = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Object res = notificationRepository.findByStuempno(fromNo);//查找是否有通知
        Object[] obj = new Object[((ArrayList<?>) res).size()];
        if (!((ArrayList<?>) res).isEmpty()) {//有通知
            try {
                //循环，获取通知内容，放到集合result2中
                for (int i = 0; i < ((ArrayList<?>) res).size(); i++) {
                    Notification notification = notificationRepository.findByFromstuempnoOrderByDateDescDistinct(fromNo).get(i);
                    int count = notificationRepository.countByFromstuempnoAndDate(fromNo, notification.getDate());
                    Account account=accountRepository.findByStuempno(notification.getStuempno()).get(0);
                    result2.put("result", "1");
                    if (count == 1) {
                        result2.clear();
                        result2.put("from", account.getName());
                        result2.put("notifyID", notification.getNotifyid());
                        result2.put("readed", notification.getReaded());
                    } else {
                        result2.put("from", "群发通知");
                        result2.put("notifyID", "群发通知");
                        result2.put("read", "群发通知");
                    }

                    result2.put("notifyHeader", notification.getNotifyheader());
                    result2.put("notifyContent", notification.getNotifycontent());
                    result2.put("date", notification.getDate());
                    result2.put("fromNo", notification.getFromstuempno());
                    result2.put("important", notification.getImportant());

                    result.put("result", "1");
                    obj[i] = JSON.toJSON(result2);
                }
            } catch (IndexOutOfBoundsException e) {
                //没有更多通知了，将result的结果位置0
                result2.clear();
                result2.put("result", "0");

                result.put("data", obj);//将获取到的数据result，转换为JSON格式，存到result2中
                JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
                return object;//返回json数据给前端
            }
            //用户未收到任何通知
        } else {
            result.put("result", "1");
            result.put("msg", "你暂时还没有发出通知呢");
        }
//        try {
//            List<Notification> notifications;
//            notifications = notificationRepository.findByFromstuempnoOrderByDateDescDistinct(fromNo);
//            for (int i = 0; i < notifications.size(); i++) {
//                Notification notification = notifications.get(i);
//                int count = notificationRepository.countByFromstuempnoAndDate(fromNo, notification.getDate());
//                result2.put("result", "1");
//                if (count != 1) {
//                    notifications.get(i).setStuempno("-1");
//                    notifications.get(i).setNotifyid(-1);
//                    notifications.get(i).setReaded(-1);
//                }
//            }
//            result.put("result", "1");
//            result.put("data", notifications);
//        } catch (Exception e) {
//            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
//            return object;//返回json数据给前端
//        }
        result.put("data", obj);
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("showNotYetReadNum")//显示某条群发通知的未读人数
    public Object showNotYetReadNum(HttpServletRequest request,
                                    @RequestBody String frontEnd) {
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        HttpSession session = request.getSession(false);

        String from;
        try {
            if (session.getAttribute("authority").equals("2")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
            from = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String date = receiveJSON.getString("date");

        int count;
        int all;
        try {
            count = notificationRepository.countByFromstuempnoAndDateAndReaded(from, date, 0);
            all = notificationRepository.countByFromstuempnoAndDate(from, date);
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }
        result.put("result", "1");
        result.put("notReadCount", count);
        result.put("shouldReadCount", all);
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("showNotYetReadList")//显示某条群发通知未读者的清单
    public Object showNotYetReadList(HttpServletRequest request,
                                     @RequestBody String frontEnd) {
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Map<String, Object> result2 = new HashMap<>();
        HttpSession session = request.getSession(false);

        String from;
        try {
            if (session.getAttribute("authority").equals("2")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
            from = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String date = receiveJSON.getString("date");

        try {
            List<Notification> notification;
            notification = notificationRepository.findByFromstuempnoAndDateAndReadedOrderByStuempnoAsc(from, date, 0);

            List<Account> accounts;
            accounts = accountRepository.findByStuempno(notification.get(0).getStuempno());
            for (int i = 1; i < notification.size(); i++)
                accounts.addAll(accountRepository.findByStuempno(notification.get(i).getStuempno()));

            result.put("result", "1");
            result2.put("data", accounts);

        } catch (Exception e) {
            result2.put("result", "0");
            result2.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }
        result2.put("result", "1");
        JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("sendToOne")//发送给单个用户
    public Object sendToOne(@RequestBody String frontEnd,
                            HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        HttpSession session = request.getSession(false);

        String fromNo;
        try {
            fromNo = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String toNo = receiveJSON.getString("toNo");
        String notifyHeader = receiveJSON.getString("notifyHeader");
        String notifyContent = receiveJSON.getString("notifyContent");
        String important = receiveJSON.getString("important");

        //检查目标用户是否存在
        try {
            accountRepository.findByStuempno(toNo).get(0);
        } catch (Exception e) {
            //不存在，返回错误信息
            result.put("result", "0");
            result.put("msg", "用户" + toNo + "不存在");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        //存在，继续操作
        try {
            Notification notification = new Notification();

            notification.setStuempno(toNo);
            notification.setNotifyheader(notifyHeader);
            notification.setNotifycontent(notifyContent);
            notification.setImportant(Integer.parseInt(important));
            notification.setFromstuempno(fromNo);

            Date DATE = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//将日期精确到具体的时分秒
            String date = formatter.format(DATE);

            notification.setDate(date);
            notification.setReaded(0);

            notificationRepository.save(notification);
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        result.put("result", "1");
        result.put("msg", "发送成功");
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("sendToMulti")
    public Object sendToMulti(@RequestBody String frontEnd,
                              HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        HttpSession session = request.getSession(false);

        String fromNo;
        try {
            if (session.getAttribute("authority").equals("2")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
            fromNo = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String authority = receiveJSON.getString("authority");
        String grade = receiveJSON.getString("grade");
        String classes = receiveJSON.getString("classes");
        String profession = receiveJSON.getString("profession");
        String department = receiveJSON.getString("department");
        String college = receiveJSON.getString("college");
        String notifyHeader = receiveJSON.getString("notifyHeader");
        String notifyContent = receiveJSON.getString("notifyContent");
        String important = receiveJSON.getString("important");

        List<?> res = accountRepository.findByAuthority(authority);

//        List<?> res=accountRepository.findUserGroup(authority, grade, classes, profession);
        //检查目标用户组是否存在
        try {
            res.get(0);
            switch (authority) {
                case "0":
                    break;
                case "1":
                    res = tutorInfoRepository.findTutorGroup(college);
                    res.get(0);
                    break;
                case "2":
                    res = studentInfoRepository.findStudentGroup(grade, classes, profession, college);
                    res.get(0);
                    break;
                case "3":
                    res = otherEmployeeInfoRepository.findEmployeeGroup(department);
                    res.get(0);
                    break;
                default:
                    throw new Exception();
            }
        } catch (Exception e) {
            //不存在，返回错误信息
            result.put("result", "0");
            result.put("msg", "用户组不存在");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        int i = 0;
        String no = "";

        Date DATE = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//将日期精确到具体的时分秒
        String date = formatter.format(DATE);

        while (true) {
            try {
//                Account account = (Account) res.get(i++);
//                no=account.getStuempno();
                switch (authority) {
                    case "1":
                        TutorInfo tutor = (TutorInfo) res.get(i++);
                        no = tutor.getEmpno();
                        break;
                    case "2":
                        StudentInfo student = (StudentInfo) res.get(i++);
                        no = student.getStuno();
                        break;
                    case "3":
                        OtherEmployeeInfo otherEmployeeInfo = (OtherEmployeeInfo) res.get(i++);
                        no = otherEmployeeInfo.getEmpno();
                        break;
                }

                notificationRepository.sendMessage(no, notifyHeader, notifyContent, date, 0, fromNo, Integer.parseInt(important));

            } catch (IndexOutOfBoundsException e) {
                result.put("result", "1");
                result.put("msg", "发送成功");
                break;
            } catch (Exception e) {
                result.put("result", "0");
                result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
                JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
                return object;//返回json数据给前端
            }
        }

        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    private void deleteDeprecateNotificationByNo(String no) {
        Date DATE = new Date();
        DATE.setDate(DATE.getDate() - 8);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(DATE);
        notificationRepository.deleteByStuempnoAndReadedAndDateLessThan(no, 1, date);
    }

    public boolean forceLogOut(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        boolean status;
        try {
            if (session == null) {
                status = false;
                return status;
            }
            if (!session.getId().equals(accountRepository.findByStuempno((String) session.getAttribute("no")).get(0).getSessionid())) {
                status = true;
                session.invalidate();
            } else {
                status = false;
            }
        } catch (Exception e) {
//        } catch (NullPointerException e) {
            status = true;
            session.invalidate();
            return status;
        }
        return status;
    }

}
